import Vue from 'vue'
import Component from './func-notification'

const CompConstructor = Vue.extend(Component)

const instances = []
let seed = 1

const notify = (options) => {
  if(Vue.prototype.$isSserver) return

  const { autoClose, ...rest } = options
  const instance = new CompConstructor({
    propsData: rest,
    data: {
      autoClose: autoClose || 3000
    }
  })
  const id = `notification_${seed++}`
  instance.id = id
  instance.vm = instance.$mount()
  document.body.appendChild(instance.vm.$el)
  instance.vm.visible = true

  let verticalOffset = 0
  instances.forEach(item=> {
    verticalOffset += item.$el.offsetHeight + 16
  })
  verticalOffset += 16
  instance.verticalOffset = verticalOffset
  instances.push(instance)

  return instance.vm
}

export default notify